package com.lenovo.smarttraffic.ui.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;

import com.lenovo.smarttraffic.R;

/**
 * Created by Caleb.F
 * on 2019-11-26
 * at 8:52
 */
public class MetroLineFragment extends DialogFragment {
    Button mBtnLine1;
    Button mBtnLine2;
    Button mBtnLine3;
    Button mBtnLine4;
    Button mBtnLine10;
    Button mBtnLineS1;
    Button mBtnLineS3;
    Button mBtnLineS8;
    private  OnItemClickListener mOnItemClickListener;

    public MetroLineFragment() {
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().setCancelable(true);
         getDialog().setCanceledOnTouchOutside(true);
        return LayoutInflater.from(getActivity()).inflate(R.layout.fragment_metro_line, container, false);

    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }

    private void initView(View convertView) {
        mBtnLine1 = convertView.findViewById(R.id.btn_line1);
        mBtnLine2 = convertView.findViewById(R.id.btn_line2);
        mBtnLine3 = convertView.findViewById(R.id.btn_line3);
        mBtnLine4 = convertView.findViewById(R.id.btn_line4);
        mBtnLine10 = convertView.findViewById(R.id.btn_line10);
        mBtnLineS1 = convertView.findViewById(R.id.btn_line_s1);
        mBtnLineS3 = convertView.findViewById(R.id.btn_line_s3);
        mBtnLineS8 = convertView.findViewById(R.id.btn_line_s8);
        final Button[] buttons=new Button[]{ mBtnLine1,mBtnLine2,mBtnLine3, mBtnLine4,mBtnLine10,
        mBtnLineS1,mBtnLineS3,mBtnLineS8};
        for (int i = 0; i <buttons.length ; i++) {
            int finalI = i;
            buttons[i].setOnClickListener(v -> {
                mOnItemClickListener.ImageClickListener((String) buttons[finalI].getText());
            });
        }
    }


    public interface OnItemClickListener{
        void  ImageClickListener(String metroLineName);
    }

    public  void setOnItemClickListener(OnItemClickListener onItemClickListener){
        mOnItemClickListener=onItemClickListener;
    }
}
